package com.example.xianglilai_end.Utils;

public class GPSTransToAMap {
    // 椭球参数
    static double pi = 3.14159265358979324;
    static double a = 6378245.0;
    static double ee = 0.00669342162296594323;

    /* 判断坐标是否在中国境内 */
    private static Boolean outOfChina(double lat, double lon){
        if (lon < 72.004 || lon > 137.8347)
            return true;
        else if (lat < 0.8293 || lat> 55.8271)
            return true;
        else
            return false;
    }

    private static double transformLat(double x, double y){
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    private static double transformLon(double x, double y){
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
        return ret;
    }

    public static String xypase(String dms, String type){
        if (dms == null || dms.equals("")) {
            return "0.0";
        }
        double result = 0.0D;
        String temp = "";

        if (type.equals("E")){
            //经度是一伦敦为点作南北两极的线为0度,所有往西和往东各180度
            String e1 = dms.substring(0,3);             //截取3位数字，经度共3位，最多180度
            String e2 = dms.substring(3, dms.length()); // 需要运算的小数

            result = Double.parseDouble(e1);
            result += (Double.parseDouble(e2) / 60.0D);
            temp = String.valueOf(result);
            if (temp.length() > 9){
                temp = e1 + temp.substring(temp.indexOf("."),9);
            }
        }else if (type.equals("N")){
            //纬度，纬度是以赤道为基准,相当于把地球分两半,两个半球面上的点和平面夹角0~90度
            String n1 = dms.substring(0, 2);            //截取2位，纬度共2位，最多90度
            String n2 = dms.substring(2, dms.length());

            result = Double.parseDouble(n1);
            result += Double.parseDouble(n2) / 60.0D;
            temp = String.valueOf(result);
            if (temp.length() > 8) {
                temp = n1 + temp.substring(temp.indexOf("."), 8);
            }
        }
        return temp;
    }


    /* 坐标转换 */
    public static double[] transform(double wgLat, double wgLon){
        double[] lat_lng = new double[2];    // 转化后的坐标
//        System.out.printf("wgLat: %s\n", wgLat);
//        System.out.printf("wgLon: %s\n", wgLon);
        if (outOfChina(wgLat, wgLon)){
            lat_lng[0] = wgLat; // 经度
            lat_lng[1] = wgLon; // 纬度
//            System.out.printf("lat_lng[0]: %s\n", lat_lng[0]);
//            System.out.printf("lat_lng[1]: %s\n", lat_lng[1]);
            return lat_lng;
        }
        double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
        double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
//        System.out.printf("dLat: %s\n", dLat);
//        System.out.printf("dLon: %s\n", dLon);
        double radLat = wgLat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        lat_lng[0] = wgLat + dLat;
        lat_lng[1] = wgLon + dLon;
//        System.out.printf("lat_lng[0]: %s\n", lat_lng[0]);
//        System.out.printf("lat_lng[1]: %s\n", lat_lng[1]);
        return lat_lng;
    }

    public static double[] transform(String gps){
        String[] str_gps = gps.split(",");
        String strNorth = xypase(str_gps[1], "N"); // 纬度
        String strEast = xypase(str_gps[3], "E");  // 经度
        double[] lat_lng = transform(Double.parseDouble(strEast), Double.parseDouble(strNorth));
         return lat_lng;
    }
}
